<?
 
require_once("mysql.php");
require_once("utilities.php");
 

function find_or_create_location($loc, $lat, $lng, $flags = 0) {


    $q = "SELECT * FROM location WHERE latstr='".normalize_float($lat)."' AND lngstr='".normalize_float($lng)."'";  
    $r = MYSQL_Q($q);
    $c = mysql_num_rows($r);

    if ($c == 0) {
    
        $q = "INSERT INTO location (count, location, latitude, longitude, latstr, lngstr, flags) ".
              "VALUES (1, '".$loc."', ".$lat." , ".$lng.", '".normalize_float($lat)."', '".normalize_float($lng)."',".($flags == 0 ? '0' : $flags).")"; 

        MYSQL_Q($q);
             
        return mysql_insert_id();
    
    } else {

        $entry = mysql_fetch_assoc($r);
        MYSQL_Q("UPDATE location SET count=".($entry['count']+1)." WHERE id=".$entry['id']);
        return $entry['id'];

    }

}

?>